#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDateTime>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    timer = new QTimer(this);
    eventfilter = new MyableEventFilter(this);
    weather = nullptr;
    music = nullptr;

    ui->labelWeather->installEventFilter(eventfilter);
    ui->label->installEventFilter(eventfilter);
    ui->label_2->installEventFilter(eventfilter);
    ui->label_3->installEventFilter(eventfilter);

    connect(this->timer,&QTimer::timeout,this,&MainWindow::updateTimer);
    connect(this->eventfilter,&MyableEventFilter::click,this,&MainWindow::openwindow);
    timer->start(1000);
    updateTimer();
}

MainWindow::~MainWindow()
{
    delete weather;
    delete music;
    delete ui;
}

void MainWindow::updateTimer()
{
    QDateTime currentTime = QDateTime::currentDateTime();
    QString timeText = currentTime.toString("hh:mm");
    ui->labelTime->setText(timeText);
    QString dateText = currentTime.toString("yyyy年MM月dd日 dddd");
    ui->labelDate->setText(dateText);
}

void MainWindow::openwindow(QObject *watched)
{
    if(watched == ui->labelWeather){
        if(weather == nullptr){
            weather = new weatherWindow(this);
        }
        weather->show();
        weather->getWeather();
        this->hide();
    }if(watched == ui->label){
        if(music == nullptr){
            music = new mainMusic(this);
        }
        music->show();
        this->hide();
    }
}
